/* Author: yangzeming
 * Date: 2014-1-10
 * Desc: 分页组件 模块化遵循CMD规范 _.Class()定义类的规范化方式
 *       组件化遵循基本的接口约定：
 *       传入的参数：option = {
                                  con : $ | dom,  //传入的dom容器
                                  template : "[String]", // 渲染的模板
                                  theme : "",    // 皮肤
                                  data : {
                                      "currentPage":"1",
                                      "pageCount":"5",
                                      "rowCount":"10"

                                  },
                                    
                              }

         initial : 初始化方法
         render ： 渲染

 */

define(function (require, exports, module) {
    /*  
    *
    */
    var Pager = _.Class({

        initial: function (option) {
            this._eventStack = {};
            var defaultOp = {
                template: '<li><a data-role="first">首页</a></li><li><a data-role="prev">上一页</a></li><li data-role="pageNum"></li><li><a data-role="next">下一页</a></li><li><a data-role="last">尾页</a></li><li data-role="pageInfo"><span class="text">总共<span data-role="rowCount"></span>条记录</span><span class="text">共<span data-role="pageCount"></span>页</li>',
                data: {
                    currentPage: 0,
                    pageCount: 0,
                    rowCount: 0
                },
                maxNum: 5,
                disabledClass: ""//"ui-state-disabled"
            };
            this.option = $.extend(true, {}, defaultOp, option);
            this.$con = $(option.con);
            this.$con.html($(this.option.template));
            this.$first = this.$con.find('[data-role="first"]');
            this.$prev = this.$con.find('[data-role="prev"]');
            this.$next = this.$con.find('[data-role="next"]');
            this.$pageNum = this.$con.find('[data-value="pageNum"]');
            this.$last = this.$con.find('[data-role="last"]');
            this.$pageInfo = this.$con.find('[data-role="pageInfo"]');
            this.$pageInput = this.$con.find('[data-target="input"]');
            this.$pageGo = this.$con.find('[data-role="go"]');
            this.unbind("changePage");
            this.renderPager(this.option.data);
            this.setCurrentPage(1);
            //this.bindEvent();
        },
        renderPager: function (data) {

            this.$con && this.$con.find('[data-target="rowCount"]').html(data.rowCount);
            this.$con && this.$con.find('[data-target="pageCount"]').html(data.pageCount);
            var currentPage = +data.currentPage,
                pageCount = +data.pageCount,
                rowCount = +data.rowCount,
                pageNum = "",
                maxNum = this.option.maxNum || 0,
                sNum = 1,
                lNum = 0;
            this.option["pageCount"] = pageCount;
            this.option["rowCount"] = rowCount;
            this.option["currentPage"] = currentPage;

            if (maxNum != 0 && pageCount > maxNum) {
                var n = parseInt((maxNum - 1) / 2, 10),
                    s = currentPage - n;
                if (s > 1) {
                    if (s + maxNum > pageCount) {
                        sNum = pageCount + 1 - maxNum;
                        lNum = maxNum;

                    } else {
                        sNum = s;
                        lNum = maxNum;
                    }
                } else {
                    sNum = 1;
                    lNum = maxNum;
                }
            } else {
                lNum = pageCount;
            }

            for (var i = sNum, len = lNum, s = 0; s < len; s++) {
                pageNum += '<a class="ui-state-default eui-pager-number" data-role="' + i + '">' + i + '</a>';
                i++;
            };
            this.$pageNum.html(pageNum);

            this.$con.undelegate('[data-role]', "click");
            this.bindEvent();

        },
        setCurrentPage: function (currentPage) {
            this.currentPage = currentPage;
            this.$pageInput && this.$pageInput.val(this.currentPage);
            this.$pageNum.find('[data-role]').removeClass("currentpage").end().find('[data-role="' + currentPage + '"]').addClass("currentpage");
            if (currentPage == 1) {
                this.$first && this.$first.addClass(this.option.disabledClass);
                this.$prev && this.$prev.addClass(this.option.disabledClass);
            } else {
                this.$first && this.$first.removeClass(this.option.disabledClass);
                this.$prev && this.$prev.removeClass(this.option.disabledClass);
            }
            if (currentPage == this.option.data["pageCount"]) {
                this.$last && this.$last.addClass(this.option.disabledClass);
                this.$next && this.$next.addClass(this.option.disabledClass);
            } else {
                this.$last && this.$last.removeClass(this.option.disabledClass);
                this.$next && this.$next.removeClass(this.option.disabledClass);
            }
        },
        getCurrentPage: function () {
            return this.currentPage;
        },
        _changePage: function (currentPage) {
            var self = this;
            if (this.option.maxNum) {

                this.trigger("changePage", currentPage, function (data) {
                    self.renderPager(data);
                    self.setCurrentPage(currentPage);
                });
            } else {

            }


        },
        getPage: function (type) {
            var currentPage = +this.currentPage,
                pageCount = this.option.data["pageCount"];
            switch (type) {
                case "first":
                    return 1;
                case "prev":
                    if (currentPage > 1)
                        return currentPage - 1;
                    else
                        return 1;
                case "go":
                    var val = +this.$pageInput.val();
                    if (val != "" && val > 0 && val <= pageCount) {
                        return val;
                    } else {
                        return currentPage;
                    }
                case "next":
                    if (currentPage + 1 < pageCount)
                        return currentPage + 1;
                    else
                        return pageCount;
                case "last":
                    return pageCount;
                default:
                    return type;

            }
        },
        bindEvent: function () {
            var self = this;
            this.$con.delegate('[data-role]', "click", function () {
                var that = $(this),
                    value = that.data("role");
                var page = +self.getPage(value);
                if (page == +self.currentPage) {
                    return;
                }
                self._changePage(page);
            });

        }


    });
    module.exports = Pager;
});